PYTHON					?= python
DJANGO_ADMIN			= django-admin
DJANGO_MANAGE 			= ./manage.py
PO_FILES				= $(wildcard ai60/*/locale/*/LC_MESSAGES/django.po)
MO_FILES				= $(PO_FILES:.po=.mo)
APP_NAMES				= commons accounts events organizations mobile
PROTOTYPE_DIR			= web_v1 web_v2 mob_v1

all: $(MO_FILES) static

po:
	@for app in $(APP_NAMES); do \
		cd ai60/$$app ; $(DJANGO_ADMIN) makemessages -l zh_CN ; cd ../../ ; \
	done

mo: $(MO_FILES)

check-translations:
	if grep -q '^#,.fuzzy' $(PO_FILES) ; then \
		echo Fuzzy translations found, aborting >&2 ; \
		exit 1 ; \
	fi

check:
	@for app in $(APP_NAMES); do \
		$(PYTHON) $(DJANGO_MANAGE) test $$app ; \
	done

static:
	if [ ! -f static/media ]; then \
		cd static ; ln -sf ../media ; cd .. ; \
	fi
	@for prototype in $(PROTOTYPE_DIR); do \
		if [ ! -f $$prototype ]; then \
			cd static ; ln -sf ../prototype/$$prototype ; cd ../ ; \
		fi \
	done

deploy: $(MO_FILE) static check-translations
	$(PYTHON) $(DJANGO_MANAGE) collectstatic --noinput

clean:
	rm -rf $(MO_FILES)
	rm -rf static/media
	@for prototype in $(PROTOTYPE_DIR); do \
		rm -rf static/$$prototype ; \
	done

.po.mo:
	msgfmt -c --statistics -o $@~ $< && mv $@~ $@

.PHONY: all clean po mo check static deploy
.SUFFIXES:
.SUFFIXES: .po .mo
